package cn.tedu.controller;

import cn.tedu.pojo.Role;
import cn.tedu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.print.DocFlavor;

@Controller
@RequestMapping("/sysadmin/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(Model model) {
        model.addAttribute("roles", roleService.findAll());
        return "sysadmin/role/RoleList";
    }

    @RequestMapping("/tocreate")
    public String tocreate() {
        return "/sysadmin/role/RoleCreate";
    }

    @RequestMapping("/save")
    public String save(Role role){
        roleService.save(role);
        //拿到所有的参数
        return "redirect:/sysadmin/role/list";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "roleId", required = false) String[] roleIds){
        if(roleIds != null){
            //执行批量删除的公共
            roleService.delete(roleIds);
        }
        return "redirect:/sysadmin/role/list";
    }

    @RequestMapping("toupdate")
    public String toupdate(Model model, String roleId){
        Role role = roleService.findRoleById(roleId);
        model.addAttribute("role",role);
        return "/sysadmin/role/RoleUpdate";
    }

    @RequestMapping("update")
    public String update(Role role){
        //通过id修改数据
        roleService.update(role);
        return "redirect:/sysadmin/role/list";
    }

    @RequestMapping("toview")
    public String toView(Model model,String roleId){
        model.addAttribute("role",roleService.findRoleById(roleId));
        return "/sysadmin/role/RoleToView";
    }
}
